# import asyncio
# from pyppeteer import launch
# from pyquery import PyQuery as pq
# 这个方法运行后不能输出内容，添加headless=False发现浏览器可以打开，但未输入网址
# async def main():
#     browser = await launch()  # 启动浏览器
#     page = await browser.newPage()  # 新建选项卡
#     await page.goto('https://sp2.scrape.center/')  # 访问页面
#     await page.waitForSelector('.item .name')   # 等待元素加载完毕
#     doc = pq(await page.content())  # 获取页面内容
#     names = [item.text() for item in doc('.item .name').items()]
#     print('Names:', names)
#     await browser.close()
#
# # asyncio.get_event_loop().run_until_complete(main())
# asyncio.run(main())

# 另外一个示例,这个示例和上面的问题一样
# import asyncio
# from pyppeteer import launch
#
#
# width, height = 1366, 768
#
# async def main():
#     browser = await launch()
#     page = await browser.newPage()
#     await page.setViewport({'width': width, 'height': height})
#     await page.goto('https://spa2.scrape.center/')
#     await page.waitForSelector('.item .name')
#     await asyncio.sleep(2)
#     await page.screenshot(path='example.png')
#     dimensions = await page.evaluate('''() => {
#         return {
#             width: document.documentElement.clientWidth,
#             height: document.documentElement.clientHeight,
#             deviceScaleFactor: window.devicePixelRatio
#         }
#     }''')
#     print(dimensions)
#     await browser.close()
#
# asyncio.run(main())

# 调试模式
# import asyncio
# from pyppeteer import launch
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe', devtools=True)
#     page = await browser.newPage()
#     await page.goto('https://www.baidu.com', waitUntil='networkidle2')
#     await asyncio.sleep(100)
#
# asyncio.run(main())

# 禁用提示条
# import asyncio
# from pyppeteer import launch
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe', headless=False, ignoreDefaultArgs=['--enable-automation'])
#     page = await browser.newPage()
#     await page.goto('https://antispider1.scrape.center/')
#     await asyncio.sleep(100)
#
# asyncio.run(main())

# 防止检测webdriver属性
# import asyncio
# from pyppeteer import launch
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe', headless=False, ignoreDefaultArgs=['--enable-automation'])
#     page = await browser.newPage()
#     await page.evaluateOnNewDocument('Object.defineProperty(navigator, "webdriver", {get: () => undefined}')
#     await page.goto('https://antispider1.scrape.center/')
#     await asyncio.sleep(100)
#
# asyncio.run(main())

# 页面设置大小
# import asyncio
# from pyppeteer import launch
# width, height = 1366, 768
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe', headless=False, ignoreDefaultArgs=['--enable-automation'], args=[f'--window-size={width},{height}'])
#     page = await browser.newPage()
#     await page.setViewport({'width': width, 'height': height})
#     await page.evaluateOnNewDocument('Object.defineProperty(navigator, "webdriver", {get: () => undefined}')
#     await page.goto('https://antispider1.scrape.center/')
#     await asyncio.sleep(100)
#
# asyncio.run(main())

# 用户数据持久化
# import asyncio
# from pyppeteer import launch
# width, height = 1366, 768
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe', headless=False, ignoreDefaultArgs=['--enable-automation'], userDataDir='./userdata', args=[f'--window-size={width},{height}'])
#     page = await browser.newPage()
#     await page.goto('https://www.pinduoduo.com')
#     await asyncio.sleep(100)
#
# asyncio.run(main())


# 开启无痕模式
# import asyncio
# from pyppeteer import launch
#
# width, height = 1366, 768
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe',
#                            headless=False,
#                            ignoreDefaultArgs=['--enable-automation'],
#                            args=[f'--window-size={width}, {height}'])
#     context = await browser.createIncognitoBrowserContext()
#     page = await context.newPage()
#     await page.setViewport({'width': width, 'height': height})
#     await page.goto('https://www.baidu.com')
#     await asyncio.sleep(5)
#     await browser.close()
# asyncio.run(main())


# Page
# 选择器
# import asyncio
# from pyppeteer import launch
# from pyquery import PyQuery as pq
#
# width, height = 1366, 768
# async def main():
#     browser = await launch(executablePath='D:\chrome-win\chrome.exe',
#                            headless=False,
#                            ignoreDefaultArgs=['--enable-automation'],
#                            args=[f'--window-size={width}, {height}'])
#     page = await browser.newPage()
#     await page.setViewport({'width': width, 'height': height})
#     await page.goto('https://spa2.scrape.center/')
#     await page.waitForSelector('.item .name')
#     j_result1 = await page.J('.item .name')
#     j_result2 = await page.querySelector('.item .name')
#     jj_result1 = await page.JJ('.item .name')
#     jj_result2 = await page.querySelectorAll('.item .name')
#     print('J result1:', j_result1)
#     print('J result2:', j_result2)
#     print('JJ result1:', jj_result1)
#     print('JJ result2:', jj_result2)
#     await browser.close()
# asyncio.run(main())

# 选项卡操作
import asyncio
from pyppeteer import launch

async def main():
    browser = await launch(executablePath='D:\chrome-win\chrome.exe',
                           headless=False,
                           ignoreDefaultArgs=['--enable-automation'])
    page = await browser.newPage()
    await page.goto('https://www.baidu.com')
    page = await browser.newPage()
    await page.goto('https://www.bing.com')
    pages = await browser.pages()
    print('Pages:', pages)
    page1 = pages[1]
    await page1.bringToFront()
    await asyncio.sleep(100)

asyncio.run(main())
